<template>
  <div class="source-execute-wrapper">
    <div class="source-execute-title">
      <span>执行目标</span>
      <div
        class="software-select-btn"
        @click.stop="reUpload"
        v-if="pathDisabled == true"
      >
        重新上传
      </div>
    </div>
    <div class="source-execute-bottom">
      <el-form
        ref="pathForm"
        :model="pathForm"
        :rules="pathRules"
        label-width="80px"
        @submit.native.prevent
      >
        <el-row :gutter="10">
          <el-col :span="20">
            <el-form-item label="安装路径" prop="uploadPath">
              <el-input
                v-model="pathForm.uploadPath"
                :disabled="pathDisabled"
                @change="changeUploadPath"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="10px">
              <el-button
                type="primary"
                :disabled="pathDisabled"
                size="mini"
                @click="submitPathForm"
                >确 认</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row style="margin-left: 80px">
        <el-steps :space="90" direction="vertical" :active="step">
          <el-step
            title="软件资源检查"
            :description="stepInfo.stepText1"
            :status="stepInfo.stepStatus1"
            :icon="step1LoadingIcon"
            v-if="selectModelRow.sourceCheck"
          >
            <template #title>
              <span>软件资源检查</span>
              <el-progress
                v-show="percent1show"
                :percentage="parseInt(fake1.progress * 100)"
                :status="step1PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <!-- selectModelRow.historySoftwareFile != null -->
          <el-step
            v-if="selectModelRow.historySoftwareFile"
            title="上传软件包"
            :description="stepInfo.stepText2"
            :status="stepInfo.stepStatus2"
            :icon="step2LoadingIcon"
          >
            <template #title>
              <span>上传软件包</span>
              <el-progress
                v-show="percent2show"
                :percentage="parseInt(fake2.progress * 100)"
                :status="step2PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historyVarScriptFile"
            title="执行环境变量脚本"
            :description="stepInfo.stepText3"
            :status="stepInfo.stepStatus3"
            :icon="step3LoadingIcon"
          >
            <template #title>
              <span>执行环境变量脚本</span>
              <el-progress
                v-show="percent3show"
                :percentage="parseInt(fake3.progress * 100)"
                :status="step3PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historySoftwareInsFile"
            title="执行安装程序脚本"
            :description="stepInfo.stepText4"
            :status="stepInfo.stepStatus4"
            :icon="step4LoadingIcon"
          >
            <template #title>
              <span>执行安装程序脚本</span>
              <el-progress
                v-show="percent4show"
                :percentage="parseInt(fake4.progress * 100)"
                :status="step4PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historyRunScriptFile"
            title="执行启动脚本"
            :description="stepInfo.stepText5"
            :status="stepInfo.stepStatus5"
            :icon="step5LoadingIcon"
          >
            <template #title>
              <span>执行启动脚本</span>
              <el-progress
                v-show="percent5show"
                :percentage="parseInt(fake5.progress * 100)"
                :status="step5PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historySoftwareFile"
            title="结束"
            :status="stepInfo.stepStatus7"
          ></el-step>
        </el-steps>
      </el-row>
    </div>
    <!-- <el-dialog
      title="软件资源检查"
      :visible.sync="modelVisible"
      width="75%"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column
          prop="item"
          label="检查项"
          width="100"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="softNeed"
          label="软件要求"
          width="100"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="monitorValue"
          label="受控端配置"
          width="100"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="valid"
          label="检查结果"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <span
              :style="{ color: scope.row.valid === '通过' ? 'green' : 'red' }"
              >{{ scope.row.valid }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="reason"
          label="原因"
          align="center"
        ></el-table-column>
      </el-table>
    </el-dialog> -->
    <a-modal
      wrapClassName="source-execute-dialog"
      destroyOnClose
      :width="'560px'"
      v-model="modelVisible"
      :footer="null"
      :maskClosable="false"
    >
      <div slot="title" class="source-execute-dialog-title">
        <span>软件资源检查</span>
      </div>
      <div class="source-execute-dialog-content">
        <div class="source-execute-detail">
          <div
            class="source-execute-detail-item"
            v-for="(it, ix) in tableData"
            :key="it.item"
          >
            <div class="source-execute-detail-item-top">
              <div class="source-execute-detail-item-top-name">
                检查项 {{ ix + 1 }}:{{ it.item }}
              </div>
              <div class="source-execute-detail-item-top-operation">
                <template v-if="it.valid == '通过'">
                  <span style="color: #67c23a">
                    <i class="el-icon-success"></i>
                    {{ it.valid }}
                  </span>
                </template>
                <template v-else>
                  <span style="color: #fc6e6e">
                    <i class="el-icon-error"></i>
                    {{ it.valid }}
                  </span>
                </template>
                <span @click.stop="toggleExpand(it)">
                  <template v-if="it.show">关闭</template>
                  <template v-else>展开详情</template>
                </span>
              </div>
            </div>
            <el-collapse-transition>
              <div class="source-execute-detail-item-bottom" v-show="it.show">
                <el-descriptions
                  title=""
                  :column="2"
                  class="source-execute-detail-item-bottom-des"
                  labelClassName="source-execute-detail-item-bottom-des-label"
                  contentClassName="source-execute-detail-item-bottom-des-content"
                >
                  <el-descriptions-item label="软件要求">{{
                    it.softNeed
                  }}</el-descriptions-item>
                  <el-descriptions-item label="受控端配置">{{
                    it.monitorValue
                  }}</el-descriptions-item>
                  <el-descriptions-item label="原因">{{
                    it.reason
                  }}</el-descriptions-item>
                </el-descriptions>
              </div>
            </el-collapse-transition>

            <!-- <div class="source-execute-detail-item-expand"></div> -->
          </div>
        </div>
      </div>
    </a-modal>
  </div>
  <!-- <div>
    <el-card shadow="hover">
      <div slot="header">
        <span>执行目标</span>
        <el-button
          v-if="pathDisabled == true"
          style="float: right; padding: 3px 0"
          type="text"
          @click="reUpload"
          >重新上传</el-button
        >
      </div>
      <el-form
        ref="pathForm"
        :model="pathForm"
        :rules="pathRules"
        label-width="80px"
      >
        <el-row :gutter="10">
          <el-col :span="20">
            <el-form-item label="安装路径" prop="uploadPath">
              <el-input
                v-model="pathForm.uploadPath"
                :disabled="pathDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="10px">
              <el-button
                type="primary"
                :disabled="pathDisabled"
                size="mini"
                @click="submitPathForm"
                >确 认</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row style="margin-top: 20px; margin-bottom: 20px; margin-left: 80px">
        <el-steps :space="100" direction="vertical" :active="step">
          <el-step
            title="软件资源检查"
            :description="stepInfo.stepText1"
            :status="stepInfo.stepStatus1"
            :icon="step1LoadingIcon"
          >
            <template #title>
              <span>软件资源检查</span>
              <el-progress
                v-show="percent1show"
                :percentage="parseInt(fake1.progress * 100)"
                :status="step1PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historySoftwareFile != null"
            title="上传软件包"
            :description="stepInfo.stepText2"
            :status="stepInfo.stepStatus2"
            :icon="step2LoadingIcon"
          >
            <template #title>
              <span>上传软件包</span>
              <el-progress
                v-show="percent2show"
                :percentage="parseInt(fake2.progress * 100)"
                :status="step2PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historyVarScriptFile != null"
            title="执行环境变量脚本"
            :description="stepInfo.stepText3"
            :status="stepInfo.stepStatus3"
            :icon="step3LoadingIcon"
          >
            <template #title>
              <span>执行环境变量脚本</span>
              <el-progress
                v-show="percent3show"
                :percentage="parseInt(fake3.progress * 100)"
                :status="step3PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historySoftwareInsFile != null"
            title="执行安装程序脚本"
            :description="stepInfo.stepText4"
            :status="stepInfo.stepStatus4"
            :icon="step4LoadingIcon"
          >
            <template #title>
              <span>执行安装程序脚本</span>
              <el-progress
                v-show="percent4show"
                :percentage="parseInt(fake4.progress * 100)"
                :status="step4PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historyRunScriptFile != null"
            title="执行启动脚本"
            :description="stepInfo.stepText5"
            :status="stepInfo.stepStatus5"
            :icon="step5LoadingIcon"
          >
            <template #title>
              <span>执行启动脚本</span>
              <el-progress
                v-show="percent5show"
                :percentage="parseInt(fake5.progress * 100)"
                :status="step5PercentStatus"
              ></el-progress>
            </template>
          </el-step>
          <el-step
            v-if="selectModelRow.historySoftwareFile != null"
            title="结束"
            :status="stepInfo.stepStatus7"
          ></el-step>
        </el-steps>
      </el-row>
    </el-card>

  </div> -->
</template>

<script>
import {
  devopsUpload,
  devopsRunVar,
  devopsRunInstall,
  devopsRunStart,
  queryHistory,
  syncSuccessProcess,
  getRulesBySoftwareIdAndVer,
} from "@/api/resource/modelSoftware.js";
import {
  validUpload,
  skIsOnline,
  getSourceStatus,
} from "@/api/resource/sever-mange.js";
import Fakeprogress from "@/utils/fakeProgress.js";
export default {
  props: {
    selectModelRow: Object,
    mainRow: Object,
  },
  data() {
    return {
      checkDic: {
        monitorValue: "受控端配置",
        softNeed: "软件要求",
        reason: "原因",
      },
      fake1: new Fakeprogress({
        timeConstant: 10000,
      }),
      fake2: new Fakeprogress({
        timeConstant: 10000,
      }),
      fake3: new Fakeprogress({
        timeConstant: 10000,
      }),
      fake4: new Fakeprogress({
        timeConstant: 10000,
      }),
      fake5: new Fakeprogress({
        timeConstant: 10000,
      }),
      stepInfo: {
        stepText1: "",
        stepText2: "",
        stepText3: "",
        stepText4: "",
        stepText5: "",
        stepStatus1: "wait",
        stepStatus2: "wait",
        stepStatus3: "wait",
        stepStatus4: "wait",
        stepStatus5: "wait",
        stepStatus7: "wait",
      },
      step1LoadingIcon: "",
      step2LoadingIcon: "",
      step3LoadingIcon: "",
      step4LoadingIcon: "",
      step5LoadingIcon: "",
      step: 0,
      // 部署路径编辑标识
      pathDisabled: false,
      pathForm: {
        uploadPath: "",
      },
      pathRules: {
        uploadPath: [
          { required: true, message: "请输入路径", trigger: "blur" },
        ],
      },
      loadingTimmer: void 0,
      successTimmer: void 0,
      step1PercentStatus: "",
      step2PercentStatus: "",
      step3PercentStatus: "",
      step4PercentStatus: "",
      step5PercentStatus: "",
      percent1show: false,
      percent2show: false,
      percent3show: false,
      percent4show: false,
      percent5show: false,
      modelVisible: false,
      tableData: [],
    };
  },
  methods: {
    changeUploadPath() {
      if (
        this.pathForm.uploadPath &&
        !this.pathForm.uploadPath.endsWith("\\")
      ) {
        this.pathForm.uploadPath = this.pathForm.uploadPath + "\\";
      }
    },
    toggleExpand(it) {
      it.show = !it.show;
    },
    reUpload() {
      this.pathDisabled = false;
      this.stepInfo = {
        stepText1: "",
        stepText2: "",
        stepText3: "",
        stepText4: "",
        stepText5: "",
        stepStatus1: "wait",
        stepStatus2: "wait",
        stepStatus3: "wait",
        stepStatus4: "wait",
        stepStatus5: "wait",
        stepStatus7: "wait",
      };
      this.step1PercentStatus = "";
      this.step2PercentStatus = "";
      this.step3PercentStatus = "";
      this.step4PercentStatus = "";
      this.step5PercentStatus = "";
      this.step1LoadingIcon = "";
      this.step2LoadingIcon = "";
      this.step3LoadingIcon = "";
      this.step4LoadingIcon = "";
      this.step5LoadingIcon = "";
      this.percent1show = false;
      this.percent2show = false;
      this.percent3show = false;
      this.percent4show = false;
      this.percent5show = false;

      this.fake1.progress = 0;
      this.fake2.progress = 0;
      this.fake3.progress = 0;
      this.fake4.progress = 0;
      this.fake5.progress = 0;
    },
    // 进度条更新
    updateProgress(percent, status) {
      let that = this;
      status = "success";
      if (percent < 90) {
        percent += 1;
        that.loadingTimmer = setTimeout(
          that.updateProgress(percent, status),
          100
        );
      } else {
        that.successTimmer = setTimeout(() => {
          percent = Math.ceil(Math.random() * 8 + 90);
        }, 2000);
      }
    },
    handleStopProgress(status) {
      clearTimeout(this.loadingTimmer);
      clearTimeout(this.successTimmer);
      status = "exception";
    },
    // 延时等待
    stepDelayTime(func) {
      return new Promise((resolve) => {
        setTimeout(async () => {
          // 根据软件类型，来拼对应的参数
          let sourceParam;
          if (this.mainRow.rjlx == 0) {
            sourceParam = {
              simulatorName: this.selectModelRow.simulatorName,
              simulatorDesc: this.selectModelRow.simulatorDesc,
              tcpPort: this.selectModelRow.tcpPort,
            };
          } else {
            sourceParam = {
              simulatorName: this.selectModelRow.softwareName,
              simulatorDesc: this.selectModelRow.historySoftwareDesc,
            };
          }

          const res = await func(
            Object.assign(
              {
                historyId: this.selectModelRow.id,
                skId: this.mainRow.skId,
                uploadPath: this.pathForm.uploadPath,
              },
              sourceParam
            )
          );
          resolve(res);
        }, 2000);
      });
    },
    submitPathForm() {
      if (this.selectModelRow == null || !this.selectModelRow.softwareId) {
        this.$message.warning("请选择软件");
        return;
      }
      console.log("selected software is :", this.selectModelRow);
      this.$refs["pathForm"].validate(async (valid) => {
        if (valid) {
          this.pathDisabled = true;
          this.step = 0;

          // 校验受控主机是否在线
          const isOnlineRes = await skIsOnline({ skId: this.mainRow.skId });
          this.$emit("changeStatus", isOnlineRes.data.isOnline);
          if (isOnlineRes.data.isOnline == "0") {
            this.$message.warning("受控主机处于离线状态");
            return;
          }
          // 校验路径是否重复执行
          const response = await validUpload({
            skIp: this.mainRow.skIp,
            skName: this.mainRow.skName,
            uploadPath: this.pathForm.uploadPath,
          });
          if (response.data && response.data.length > 0) {
            this.$message.warning("不要输入重复路径");
            return;
          }
          // 根据软件的 资源检查规则 验证路径是否满足
          // 校验windows的
          //if (this.mainRow.os.indexOf('Windows')!=-1) {

          if (
            this.mainRow.os.indexOf("Windows") != -1 ||
            this.mainRow.os.indexOf("windows") != -1
          ) {
            this.step++;
            this.stepInfo.stepStatus1 = "process";
            this.step1LoadingIcon = "el-icon-loading";
            // 进度条
            this.step1PercentStatus = "";
            this.percent1show = true;
            this.fake1.progress = 0;
            this.fake1.start();

            const validRules = await this.validWindowsSoftRules();
            if (!validRules) {
              this.modelVisible = true;
              this.fake1.stop();
              this.stepInfo.stepStatus1 = "error";
              this.stepInfo.stepText1 = "检查不通过";
              this.handleStopProgress(this.step1PercentStatus);
              return;
            } else {
              this.fake1.end();
              this.stepInfo.stepText1 = "检查通过";
              this.stepInfo.stepStatus1 = "success";
              this.step1LoadingIcon = "";
            }
          } else {
            this.step++;
            this.stepInfo.stepStatus1 = "process";
            this.step1LoadingIcon = "el-icon-loading";
            // 进度条
            this.step1PercentStatus = "";
            this.percent1show = true;
            this.fake1.progress = 0;
            this.fake1.start();

            const validRules = await this.validLinuxSoftRules();
            if (!validRules) {
              this.modelVisible = true;
              this.fake1.stop();
              this.stepInfo.stepStatus1 = "error";
              this.stepInfo.stepText1 = "检查不通过";
              this.handleStopProgress(this.step1PercentStatus);
              return;
            } else {
              this.fake1.end();
              this.stepInfo.stepText1 = "检查通过";
              this.stepInfo.stepStatus1 = "success";
              this.step1LoadingIcon = "";
            }
          }

          // 请求改为同步按顺序执行
          // 软件安装包上传
          // this.selectModelRow.historySoftwareFile != null
          if (this.selectModelRow.historySoftwareFile) {
            this.step++;
            this.stepInfo.stepStatus2 = "process";
            this.step2LoadingIcon = "el-icon-loading";
            // 进度条
            // this.step1Percent = 0
            this.step2PercentStatus = "";
            this.percent2show = true;
            this.fake2.progress = 0;
            this.fake2.start();
            // 设置受控端软件部署标志为开始状态
            this.$store.commit("SET_DEPLOY_FLAG", true);
            this.$store.commit("SET_CONTROLL_ID", this.mainRow.skId);
            // this.updateProgress(this.step1Percent, this.step2PercentStatus)
            const { code, msg } = await this.stepDelayTime(devopsUpload);
            if (code == 200) {
              this.fake2.end();
              this.stepInfo.stepText2 = "上传成功";
              this.stepInfo.stepStatus2 = "success";
              this.step2LoadingIcon = "";
            } else {
              // 进程失败
              // 设置受控端软件部署标志为停止状态
              this.$store.commit("SET_DEPLOY_FLAG", false);
              this.fake2.stop();
              this.stepInfo.stepStatus2 = "error";
              this.stepInfo.stepText2 = msg;
              this.handleStopProgress(this.step2PercentStatus);
              return;
            }
          }
          // 环境变量脚本
          if (this.selectModelRow.historyVarScriptFile) {
            this.step++;
            this.stepInfo.stepStatus3 = "process";
            this.step3LoadingIcon = "el-icon-loading";
            // 进度条
            // this.step2Percent = 0
            this.step3PercentStatus = "";
            this.percent3show = true;
            this.fake3.progress = 0;
            this.fake3.start();
            // this.updateProgress(this.step2Percent, this.step3PercentStatus)
            const { code, msg } = await this.stepDelayTime(devopsRunVar);
            if (code == 200) {
              this.fake3.end();
              // this.step2Percent = 100
              this.stepInfo.stepText3 = "执行成功";
              this.stepInfo.stepStatus3 = "success";
              this.step3LoadingIcon = "";
            } else {
              // 进程失败
              // 设置受控端软件部署标志为停止状态
              this.$store.commit("SET_DEPLOY_FLAG", false);
              this.fake3.stop();
              this.stepInfo.stepStatus3 = "error";
              this.stepInfo.stepText3 = msg;
              this.handleStopProgress(this.step3PercentStatus);
              return;
            }
          }
          // 安装程序脚本
          if (this.selectModelRow.historySoftwareInsFile) {
            this.step++;
            this.stepInfo.stepStatus4 = "process";
            this.step4LoadingIcon = "el-icon-loading";
            // 进度条
            // this.step3Percent = 0
            this.step4PercentStatus = "";
            this.percent4show = true;
            this.fake4.progress = 0;
            this.fake4.start();
            // this.updateProgress(this.step3Percent, this.step4PercentStatus)
            const { code, msg } = await this.stepDelayTime(devopsRunInstall);
            if (code == 200) {
              this.fake4.end();
              // this.step3Percent = 100
              this.stepInfo.stepText4 = "执行成功";
              this.stepInfo.stepStatus4 = "success";
              this.step4LoadingIcon = "";
            } else {
              // 进程失败
              // 设置受控端软件部署标志为停止状态
              this.$store.commit("SET_DEPLOY_FLAG", false);
              this.fake4.stop();
              this.stepInfo.stepStatus4 = "error";
              this.stepInfo.stepText4 = msg;
              this.handleStopProgress(this.step4PercentStatus);
              return;
            }
          }
          // 执行启动脚本
          if (this.selectModelRow.historyRunScriptFile) {
            this.step++;
            this.stepInfo.stepStatus5 = "process";
            this.step5LoadingIcon = "el-icon-loading";
            // 进度条
            // this.step4Percent = 0
            this.step5PercentStatus = "";
            this.percent5show = true;
            this.fake5.progress = 0;
            this.fake5.start();
            // this.updateProgress(this.step4Percent, this.step5PercentStatus)
            const { code, msg } = await this.stepDelayTime(devopsRunStart);
            if (code == 200) {
              this.fake5.end();
              // this.step4Percent = 100
              this.stepInfo.stepText5 = "执行成功";
              this.stepInfo.stepStatus5 = "success";
              this.step5LoadingIcon = "";
            } else {
              // 进程失败
              // 设置受控端软件部署标志为停止状态
              this.$store.commit("SET_DEPLOY_FLAG", false);
              this.fake5.stop();
              this.stepInfo.stepStatus5 = "error";
              this.stepInfo.stepText5 = msg;
              this.handleStopProgress(this.step5PercentStatus);
              return;
            }
          }
          setTimeout(async () => {
            this.stepInfo.stepStatus7 = "success";
            this.$emit("executeOk");
            if ((this.stepInfo.stepStatus7 = "success")) {
              // 所有进程完成
              // 设置受控端软件部署标志为停止状态
              this.$store.commit("SET_DEPLOY_FLAG", false);
              let param = {
                skId: this.mainRow.skId,
              };
              let sucessData = await queryHistory(param);
              let newParm;
              if (this.mainRow.rjlx == 0) {
                newParm = { softwareType: this.mainRow.rjlx };
              } else {
                newParm = {
                  softwareType: this.mainRow.rjlx,
                  simulatorDesc: this.selectModelRow.historySoftwareDesc,
                  simulatorName: this.selectModelRow.softwareName,
                };
              }
              syncSuccessProcess(
                Object.assign(sucessData.rows[0], newParm)
              ).then((res) => {});
            }
          }, 2000);
        }
      });
    },
    // 验证资源检查规则
    async validWindowsSoftRules() {
      let res = true;
      let errorMsg = [];
      let checkList = [];
      let monitor = {};
      await getRulesBySoftwareIdAndVer({
        softwareId: this.selectModelRow.softwareId,
        ver: this.selectModelRow.softwareVersion,
      }).then((res) => {
        checkList = res.data;
        console.log("==============>软件资源规则：" + JSON.stringify(res.data));
      });
      await getSourceStatus({
        name: this.mainRow.skName,
        ip: this.mainRow.skIp,
      }).then((res) => {
        monitor = res.data;
      });
      checkList.forEach((it) => {
        if (it.item === "磁盘") {
          // 查看安装路径是哪个盘
          const softwareDisk = this.pathForm.uploadPath
            .substring(0, 1)
            .toUpperCase();
          let diskinfoarr = monitor.monitorDisk.filter((md) =>
            md.pflj.toUpperCase().startsWith(softwareDisk)
          );
          // 先判断有没有该盘符
          if (this.validatenull(diskinfoarr)) {
            res = false;
            // errorMsg.push("受控端没有" + softwareDisk + "盘，无法安装")
            errorMsg.push({
              item: it.item,
              softNeed: it.minvalue + " " + it.unit,
              monitorValue: "",
              valid: "不通过",
              reason: "受控端没有" + softwareDisk + "盘，无法安装",
            });
          } else {
            // 判断硬盘大小能不能满足软件要求
            const diskinfo = diskinfoarr[0];
            // 转换成GB比较
            const minValue =
              it.unit === "MB" ? it.minvalue / 1024 : it.minvalue;
            let monitorDiskSize = diskinfo.kydx.includes("MB")
              ? diskinfo.kydx.split(" ")[0] / 1024
              : diskinfo.kydx.split(" ")[0];

            if (Number(minValue) > Number(monitorDiskSize)) {
              res = false;
              // errorMsg.push("受控端" + softwareDisk + "盘剩余" + diskinfo.kydx + "不满足软件的磁盘要求(" + (it.minvalue + it.unit) + ")，无法安装")
              errorMsg.push({
                item: it.item,
                softNeed: it.minvalue + " " + it.unit,
                monitorValue: diskinfo.kydx,
                valid: "不通过",
                reason:
                  "受控端" +
                  softwareDisk +
                  "盘剩余" +
                  diskinfo.kydx +
                  "不满足软件的磁盘要求(" +
                  (it.minvalue + it.unit) +
                  ")，无法安装",
              });
            } else {
              errorMsg.push({
                item: it.item,
                softNeed: it.minvalue + " " + it.unit,
                monitorValue: diskinfo.kydx,
                valid: "通过",
                reason: "",
              });
            }
          }
        } else if (it.item === "内存") {
          // 将规则数值调整到GB比较
          const minValue = it.unit === "MB" ? it.minvalue / 1024 : it.minvalue;
          const monitorValue = monitor.monitorMemory.znc;
          if (Number(minValue) > Number(monitorValue)) {
            res = false;
            // errorMsg.push("受控端内存(" + monitorValue + "GB)不满足软件最低的内存要求(" + (it.minvalue + it.unit) + ")，无法安装")
            errorMsg.push({
              item: it.item,
              softNeed: it.minvalue + " " + it.unit,
              monitorValue: monitor.monitorMemory.znc,
              valid: "不通过",
              reason:
                "受控端内存(" +
                monitorValue +
                "GB)不满足软件最低的内存要求(" +
                (it.minvalue + it.unit) +
                ")，无法安装",
            });
          } else {
            errorMsg.push({
              item: it.item,
              softNeed: it.minvalue + " " + it.unit,
              monitorValue: monitor.monitorMemory.znc,
              valid: "通过",
              reason: "",
            });
          }
        }
      });
      if (!res) {
        // this.$message({
        //   showClose: false,
        //   dangerouslyUseHTMLString: true,
        //   message: errorMsg.join("<br>"),
        //   type: 'error'
        // });
        this.tableData = errorMsg.map((item, index) => {
          return {
            ...item,
            show: index == 0 ? true : false,
          };
        });
        console.log("获得tabel", this.tableData);
      }
      return res;
    },
    // 验证资源检查规则
    async validLinuxSoftRules() {
      let res = true;
      let errorMsg = [];
      let checkList = [];
      let monitor = {};
      await getRulesBySoftwareIdAndVer({
        softwareId: this.selectModelRow.softwareId,
        ver: this.selectModelRow.softwareVersion,
      }).then((res) => {
        checkList = res.data;
        console.log("==============>软件资源规则：" + JSON.stringify(res.data));
      });
      await getSourceStatus({
        name: this.mainRow.skName,
        ip: this.mainRow.skIp,
      }).then((res) => {
        monitor = res.data;
      });
      checkList.forEach((it) => {
        if (it.item === "磁盘") {
          // 按照盘符路径长度排序 长的在前
          let diskinfoarr = JSON.parse(JSON.stringify(monitor.monitorDisk));
          diskinfoarr.sort((a, b) => {
            return b.pflj.length - a.pflj.length;
          });
          const softwarePath = this.pathForm.uploadPath;
          for (let j = 0; j < diskinfoarr.length; j++) {
            const element = diskinfoarr[j];
            if (softwarePath.indexOf(element.pflj) === 0) {
              // 转换成GB比较
              const minValue =
                it.unit === "MB" ? it.minvalue / 1024 : it.minvalue;
              let monitorDiskSize = element.kydx.includes("MB")
                ? element.kydx.split(" ")[0] / 1024
                : element.kydx.split(" ")[0];

              if (Number(minValue) > Number(monitorDiskSize)) {
                res = false;
                // errorMsg.push("受控端所选磁盘剩余" + element.kydx + "不满足软件的磁盘要求(" + (it.minvalue + it.unit) + ")，无法安装")
                errorMsg.push({
                  item: it.item,
                  softNeed: it.minvalue + " " + it.unit,
                  monitorValue: element.kydx,
                  valid: "不通过",
                  reason:
                    "受控端所选磁盘剩余" +
                    element.kydx +
                    "不满足软件的磁盘要求(" +
                    (it.minvalue + it.unit) +
                    ")，无法安装",
                });
              } else {
                errorMsg.push({
                  item: it.item,
                  softNeed: it.minvalue + " " + it.unit,
                  monitorValue: element.kydx,
                  valid: "通过",
                  reason: "",
                });
              }

              break;
            }
          }
        } else if (it.item === "内存") {
          // 将规则数值调整到GB比较
          const minValue = it.unit === "MB" ? it.minvalue / 1024 : it.minvalue;
          const monitorValue = monitor.monitorMemory.znc;
          if (Number(minValue) > Number(monitorValue)) {
            res = false;
            // errorMsg.push("受控端内存(" + monitorValue + "GB)不满足软件最低的内存要求(" + (it.minvalue + it.unit) + ")，无法安装")
            errorMsg.push({
              item: it.item,
              softNeed: it.minvalue + " " + it.unit,
              monitorValue: monitorValue + "GB",
              valid: "不通过",
              reason:
                "受控端内存(" +
                monitorValue +
                "GB)不满足软件最低的内存要求(" +
                (it.minvalue + it.unit) +
                ")，无法安装",
            });
          } else {
            errorMsg.push({
              item: it.item,
              softNeed: it.minvalue + " " + it.unit,
              monitorValue: monitorValue + "GB",
              valid: "通过",
              reason: "",
            });
          }
        }
      });
      if (!res) {
        // this.$message({
        //   showClose: false,
        //   dangerouslyUseHTMLString: true,
        //   message: errorMsg.join("<br>"),
        //   type: 'error'
        // });
        this.tableData = errorMsg;
      }
      return res;
    },
  },
};
</script>

<style lang="scss" scoped>
.source-execute-wrapper {
  border: 1px solid #e5e5e5;
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  .source-execute-title {
    padding: 14px 14px 14px 30px;
    border-bottom: 1px solid #e7e7e7;
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      position: relative;
      padding-left: 12px;
      font-size: 18px;
      color: #333333;
      &::before {
        content: "";
        position: absolute;
        width: 4px;
        height: 98%;
        top: 50%;
        transform: translateY(-50%);
        left: 0;
        background: #1486f6;
      }
    }
    .software-select-btn {
      color: #1890ff;
      font-size: 14px;
      cursor: pointer;
    }
  }
  .source-execute-bottom {
    flex: 1;
    overflow: auto;
    padding: 30px 30px 0 30px;
  }
}
</style>
<style lang="scss">
.source-execute-dialog {
  display: flex;
  justify-content: center;
  align-items: center;
  .ant-modal {
    top: 0;
  }
  .ant-modal-content {
    // height: 400px;
    display: flex;
    flex-direction: column;
    .ant-modal-body {
      flex: 1;
      overflow: auto;
    }
  }
  .source-execute-dialog-title {
    span {
      position: relative;
      padding-left: 12px;
      font-size: 18px;
      color: #333333;
      line-height: 20px;
      &::before {
        content: "";
        position: absolute;
        width: 4px;
        height: 98%;
        top: 50%;
        transform: translateY(-50%);
        left: 0;
        background: #1486f6;
      }
    }
  }
  .source-execute-dialog-content {
    .source-execute-detail {
      border: 1px solid #dfe6ec;
      .source-execute-detail-item {
        border-top: 1px solid #dfe6ec;
        &:first-child {
          border-top: none;
        }
        .source-execute-detail-item-top {
          display: flex;
          &-name {
            width: 150px;
            padding: 10px 0 10px 12px;
            background: #f5f7fa;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            line-height: 20px;
          }
          &-operation {
            padding: 0 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex: 1;
            overflow: hidden;
            span {
              font-size: 14px;
              &:first-child {
                // color: #fc6e6e;
              }
              &:last-child {
                color: #1b90ff;
                cursor: pointer;
              }
            }
          }
        }
        .source-execute-detail-item-bottom {
          border-top: 1px solid #dfe6ec;
          padding: 15px 20px;
        }
        .source-execute-detail-item-bottom-des-label {
          width: 110px;
          color: #333333;
        }
        .source-execute-detail-item-bottom-des-content {
          color: #777777;
        }
      }
    }
  }
}
</style>
